package org.jeecg.modules.bobo.common.util;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @program: jeecg-boot-parent
 * @description:
 * @author: 三千霜
 * @create: 2020-08-13 09:57
 */

public class BeanUtils extends org.springframework.beans.BeanUtils {
    public static String[] getNullPropertyNames (Object source) {
    final BeanWrapper src = new BeanWrapperImpl(source);
    java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

    Set<String> emptyNames = new HashSet<String>();
    for(java.beans.PropertyDescriptor pd : pds) {
        Object srcValue = src.getPropertyValue(pd.getName());
        if (srcValue == null) {
            emptyNames.add(pd.getName());
        }
    }
    String[] result = new String[emptyNames.size()];
    return emptyNames.toArray(result);
    }

    public static void copyProperties(Object src, Object target) {
        copyProperties(src, target, getNullPropertyNames(src));
    }
    public static <T,K> List<K> copyPropertiesList(List<T> srcs, Class<K> clazz) {
        List<K> targets=new ArrayList<>();
        for (T s :
                srcs) {
            K target= null;
            try {
                target = clazz.newInstance();
                copyProperties(s, target);
                targets.add(target);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return targets;
    }

}
